<template>
  <div class="city_main">
    <!--头部-->
    <head-top head-title="城市选择"></head-top>
    <!--城市搜索-->
    <!-- <div class="search-city">
      <input type="text" placeholder="搜索城市名">
      <span class="search-icon">
        <img src="./images/search@2x.png" alt="">
      </span>
    </div>
    <div class="line"></div> -->
    <!--当前城市-->
    <div class="back" @click="goback">
      <img src="../header//images/fanhui@2x.png" alt="">
    </div>
    <div class="current_city">
      <p>当前城市</p>
      <div class="location">
        <img src="./images/dingwei1@2x.png" alt="">
        <span v-html="title"></span>
      </div>
    </div>
    <!--可选区域-->
    <div class="line"></div>
    <div class="chose_area">
      <div class="chose_header">
        <span>可选区域</span>
      </div>
      <div class="area_chose clearfix">
        <ul>
          <li v-for="(item,index) in bills" :key="index" @click="selectStyle(item, item.id) "
              :class="{'active':item.active,'unactive':!item.active}">
            <h4 ref="title">{{item.title}}</h4>
          </li>
        </ul>
      </div>
    </div>
    <!--热门城市-->
    <div class="line"></div>
    <div class="hot_city clearfix">
      <div class="hot_header">
        <span>热门城市</span>
      </div>
      <div class="hot_chose clearfix">
        <ul>
          <li v-for="(item,index) in lists"
          :key="index"
          @click="getStreet(item.id,item)"
          :class="{'active':item.active,'unactive':!item.active}"
          >
            <p>{{item.title}}</p>
          </li>
        </ul>
      </div>
    </div>
  </div>
</template>
<script>
  import headTop from '../header/Header.vue'
  // import commonUrl from '../../common/js/commonUrl.js'
  import {saveCity,
          saveCityId,
          loadCity,
          loadCityId
          } from '../../common/js/cache.js'
  import {getCity, getStreetData, getStreetDefault} from '../../api/cityVillage.js'
  export default {
    name: 'ChoseCity',
    data () {
      return {
        bills: [],
        lists: [],
        title: loadCity(),
        cityId: {
          type: Number,
          default: loadCityId()
        }
      }
    },
    created() {
      this._getCity()
      this._getStreetDefault()
    },
    methods: {
      goback() {
        this.$router.go(-1)
        this.$router.push({
          path: '/home/ChosePlot/' + loadCityId()
        })
      },
      selectStyle (item) {
        setTimeout(() => {
          this.bills.forEach(item => {
            this.$set(item, 'active', false)
          })
          this.$set(item, 'active', true)
        }, 20)
        this.$router.push({
          path: '/home/ChosePlot/' + item.id
        })
      },
      // 获取城市
      _getCity() {
        getCity().then(res => {
          if (res.code === 200) {
            this.lists = res.data
          }
        })
      },
      // 根据城市获取各区域
      getStreet(itemId, item) {
        getStreetData(itemId).then(res => {
          if (res.code === 200) {
            this.bills = res.data
          }
        })
        setTimeout(() => {
          this.lists.forEach((item, index) => {
            this.$set(item, 'active', false)
          })
          this.$set(item, 'active', true)
          this.cityId = item.id
          this.title = item.title
          saveCity(item.title)
          saveCityId(item.id)
        }, 20)
      },
      // 获取默认的区域
      _getStreetDefault () {
        getStreetDefault().then(res => {
          this.bills = res.data
        })
      }
    },
    components: {
      headTop
    }
  }
</script>
<style lang="scss" scoped>
  @import "../../common/sass/index.scss";

  .city_main {
    width: 100%;
    padding-top: 40px;
    background: #fff;
    position: relative;
    .back{
      position: absolute;
      top: 0;
      left: 0;
      width: 40px;
      height: 40px;
      line-height: 40px;
      z-index: 667;
      img{
        width: 9px;
        height: 16px;
        padding-left: 10px;
        padding-top: 13px;
      }
    }
    .search-city {
      text-align: center;
      padding: 5px 0;
      position: relative;
      input {
        height: 37px;
        border-radius: 5px;
        outline: none;
        border: none;
        width: 90%;
        background: #e6e6e6;
        text-align: center;
      }
      .search-icon {
        position: absolute;
        left: 60%;
        top: 30%;
        img {
          width: 17px;
          height: 17px;
        }
      }
    }
    .line {
      width: 100%;
      height: 10px;
      background: #f2f2f2;
    }
    .current_city {
      width: 100%;
      height: 72px;
      p {
        font-size: 15px;
        padding-top: 15px;
        padding-left: 10px;
      }
      .location {
        img {
          width: 13px;
          height: 17px;
          float: left;
          margin-left: 10px;
          padding-top: 10px;
        }
        span {
          font-size: 13px;
          color: #0078de;
          float: left;
          margin-left: 10px;
          line-height: 40px;
        }
      }
    }
    /*可选区域部分的样式*/
    .chose_area {
      width: 100%;
      .chose_header {
        width: 100%;
        height: 44px;
        border-bottom: 1px solid #d7d7d7;
        span {
          font-size: 15px;
          line-height: 44px;
          margin-left: 10px;
        }
      }
      .area_chose {
        margin: 0 10px;
        padding-bottom: 10px;
        height: 150px;
        ul {
          li {
            float: left;
            margin-top: 10px;
            margin-right: 5px;
            text-align: center;
            padding: 0 10px;
            h4 {
              font-size: 13px;
              font-weight: 400;
              line-height: 40px;
            }
          }
        }
      }
    }
    .active {
      color: #0078de;
      border: 1px solid #0078de;
      border-radius: 5px;
    }
    .unactive {
      color: #292929;
      border: 1px solid #d9d9d9;
      border-radius: 5px;
    }
    /*热门城市部分的样式*/
    .hot_city {
      width: 100%;
      .hot_header {
        width: 100%;
        height: 44px;
        border-bottom: 1px solid #d7d7d7;
        span {
          font-size: 15px;
          line-height: 44px;
          margin-left: 10px;
        }
      }
      .hot_chose {
        padding: 10px;
        ul {
          li {
            height: 36px;
            float: left;
            margin-right: 10px;
            text-align: center;
            border-radius: 5px;
            margin-bottom: 5px;
            p {
              line-height: 36px;
              font-size: 13px;
              padding-left: 20px;
              padding-right: 20px;
            }
          }
        }
      }
    }

    /*城市列表*/
    .city_one {
      .letter {
        width: 100%;
        height: 40px;
        background: #d9d9d9;
        margin-bottom: 10px;
        span {
          font-size: 15px;
          line-height: 40px;
          padding-left: 10px;
        }
      }
      .city_list {
        width: 100%;
        ul {
          li {
            width: 100%;
            height: 40px;
            border-bottom: 1px solid #d7d7d7;
            span {
              line-height: 40px;
              font-size: 15px;
              margin-left: 10px;
            }
          }
        }
      }
    }
  }
</style>
